<?php

namespace App\Http\Controllers;

use App\Models\City;
use App\Models\Factory;
use Illuminate\Http\Request;

class CityController extends Controller
{
    public function list(Request $request)
    {
        $list = \App\Models\City::with('children')
            ->where('parent_id', 0)
            ->get();
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => $list
        ];
    }


    public function default(Request $request)
    {
        $lat = $request->input('lat');
        $lng = $request->input('lng');
        if ($lat) {
            $city = \App\Models\City::where('parent_id', 0)
                ->orderByRaw("ST_Distance_Sphere(point(lng, lat), point({$lng}, {$lat}))")
                ->first();
        } else {
            $city = \App\Models\City::where('parent_id', 0)
                ->orderBy('id')
                ->first();
        }
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => $city
        ];
    }

    /**
     * 根据城市查找街区
     * @return void
     */
    public function town(Request $request)
    {
        $q = $request->input('q');
        $city = City::where('parent_id', $q)->get();

        return $city->map(function ($item) {
            /**
             * @var City $item
             */
            return [
                'id' => $item->id,
                'text' => $item->name
            ];
        });
    }

    /**
     * 地图找房第一层
     * @param Request $request
     * @return void
     */
    public function factoryList(Request $request)
    {
        $type = $request->input('type', 1);
        $city_id = $request->input('city_id', 2);
        $citys=City::where('parent_id',$city_id)->get();
        $factory = Factory::where('type', $type)
            ->where('city_id', $city_id)
            ->selectRaw('count(*) as num,town_id')
            ->groupBy('town_id')
            ->get();
        $factory = $factory->pluck('num', 'town_id');
        $citys = $citys->map(function ($item) use ($factory) {
            /**
             * @var City $item
             */
            return [
                'id' => $item->id,
                'city_name' => $item->name,
                'num' => $factory[$item->id] ?? 0,
                'lat'=>$item->lat,
                'lng'=>$item->lng
            ];
        });
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => $citys
        ];
    }
    public function second(Request $request)
    {
        $type = $request->input('type', 1);
        $city_id = $request->input('city_id', 2);
        $town_id = $request->input('town_id', 2);
        $factory = Factory::where('type', $type)
            ->where('city_id', $city_id)
            ->where('town_id', $town_id)
            ->get();
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => $factory->map(function ($item){
                /**
                 * @var Factory $item
                 */
                return [
                    'id'=>$item->id,
                    'title'=>$item->title,
                    'lat'=>$item->baidu_lat,
                    'lng'=>$item->baidu_lng,
                ];
            })
        ];
    }

}
